package com.boot.frame.jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.HashMap;
import java.util.Map;

public class JwtToken {

    // token 私钥
    private static final String TOKEN_SECRET = "f26e587c28064d0e855e72c0a6a0e618";

    /**
     * 生成token
     * @param userId
     * @return
     */
    public static String createToken(Integer userId){
        String date = String.valueOf(System.currentTimeMillis());
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        Map<String, Object> header = new HashMap<>(2);
        header.put("typ","JWT");
        header.put("alg","HS256");
        String strToken = JWT.create()
                .withHeader(header)
                .withClaim("id", userId)
                .withClaim("date", date)
                .sign(algorithm);
        return strToken;
    }

    public static void main(String[] args) {
        System.out.println(createToken(1239));
    }

}
